package com.ethan.cas;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

import java.util.Date;

/**
 * author ethan
 * create 2018-03-30 上午10:17
 * desc
 **/
public class CASTest{
    public static Order order = new Order(1,20,1,"1522376803571");
    public static void main(String[] args){
        System.out.println("start:"+order.toString());

        Runnable task = () ->{
            String thradName = Thread.currentThread().getName();
            System.out.println("***"+thradName);
            updateOrderStatus(2,"1522376803571","1522378320002");
            System.out.println("->"+order.toString());
        };
        task.run();

        Runnable task2 = () ->{
            String thradName = Thread.currentThread().getName();
            System.out.println("==="+thradName);
            updateOrderStatus(3,"1522376803571","1522378320003");
            System.out.println("->"+order.toString());
        };
        task2.run();

        Runnable task3 = () ->{
            String thradName = Thread.currentThread().getName();
            System.out.println("==="+thradName);
            updateOrderStatus(4,order.getUpdateTime(),"1522378320004");
            System.out.println("->"+order.toString());
        };
        task3.run();

        Runnable task4 = () ->{
            String thradName = Thread.currentThread().getName();
            System.out.println("==="+thradName);
            updateOrderStatus(5,order.getUpdateTime(),"1522378320005");
            System.out.println("->"+order.toString());
        };
        task4.run();

        Thread thread = new Thread(task);
        thread.start();

        Thread thread2 = new Thread(task2);
        thread2.start();

        Thread thread3 = new Thread(task3);
        thread3.start();

        Thread thread4 = new Thread(task4);
        thread4.start();

        System.out.println("end:"+order.toString());
    }

    public static boolean updateOrderStatus(int status,String updateTime,String VoldupdateTime){
        String oldUpdateTime = order.getUpdateTime();
        if(updateTime.equals(oldUpdateTime)){
            //long time = new Date().getTime();
            order.setUpdateTime(VoldupdateTime);
            order.setStatus(status);
            return true;
        }
        return false;
    }

}


@Data
@AllArgsConstructor
@ToString
class Order{
    private int id;
    private double price;
    private int status;
    private String updateTime;
}